package com.flyou.ccnucenter.ui.activity.henuBeside.note;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.Note;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.widget.ClearEditText;

import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class AddNoteActivity extends BaseActivity1 implements View.OnClickListener, TextWatcher {
    private int selectionStart;
    private int selectionEnd;
    private int totalNum = 3000;
    private CharSequence tempCount;
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private TextView titleBarRight;
    private ClearEditText title;
    private EditText content;
    private TextView textNum;
    private String intentTitle;
    private String intentContent;
    private String intentObjectId;
    private int intentPosition;
    public static final int ACTIVITY_UPDATE = 100;
    public static final int ACTVIITY_SAVE = 101;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_note);

        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        titleBarRight = (TextView) findViewById(R.id.titlebar_tv_text_right);
        title = (ClearEditText) findViewById(R.id.tv_note_title);
        content = (EditText) findViewById(R.id.tv_note_content);
        textNum = (TextView) findViewById(R.id.tv_love_nubmer);


    }

    @Override
    protected void initDate() {
        intentTitle = getIntent().getStringExtra("title");
        intentContent = getIntent().getStringExtra("content");
        intentObjectId = getIntent().getStringExtra("objectId");
        intentPosition = getIntent().getIntExtra("position", 0);
        if (TextUtils.isEmpty(intentTitle)) {
            mTitlebarTvTitle.setText("新增笔记");
            titleBarRight.setText("保存");
            titleBarRight.setVisibility(View.VISIBLE);
        } else {
            mTitlebarTvTitle.setText("编辑笔记");
            titleBarRight.setText("保存");
            titleBarRight.setVisibility(View.VISIBLE);
            title.setText(intentTitle);
            content.setText(intentContent);
            textNum.setText(intentContent.length() + "/" + totalNum);
        }
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        titleBarRight.setOnClickListener(this);
        content.addTextChangedListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.titlebar_tv_text_right:
                if (TextUtils.isEmpty(title.getText().toString().trim())) {
                    Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(content.getText().toString().trim())) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                addNote();
                break;
        }
    }


    /*
     * 获取 笔记数据
     */
    private void addNote() {
        String userId = (String) SPUtils.get(AddNoteActivity.this, "objectId", "");
        if (TextUtils.isEmpty(intentTitle)) {


            final Note note = new Note(userId, title.getText().toString().trim(), content.getText().toString().trim());
            note.save(AddNoteActivity.this, new SaveListener() {
                @Override
                public void onStart() {
                    mProgressDialog = ProgressDialog.show(AddNoteActivity.this, null, "正在保存笔记", false, true);
                    super.onStart();
                }

                @Override
                public void onFinish() {
                    mProgressDialog.dismiss();
                    super.onFinish();
                }

                @Override
                public void onSuccess() {
                    Intent intent = getIntent();
                    intent.putExtra("note", note);
                    setResult(ACTVIITY_SAVE, intent);
                    finish();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(AddNoteActivity.this, BmobErrorCodeUtils.getMessage(i, s), Toast.LENGTH_SHORT).show();

                }
            });
        } else {
            final Note note = new Note(userId, title.getText().toString().trim(), content.getText().toString().trim());
            note.update(AddNoteActivity.this, intentObjectId, new UpdateListener() {
                @Override
                public void onStart() {
                    mProgressDialog = ProgressDialog.show(AddNoteActivity.this, null, "正在保存笔记", false, true);
                    super.onStart();
                }

                @Override
                public void onFinish() {
                    mProgressDialog.dismiss();
                    super.onFinish();
                }

                @Override
                public void onSuccess() {
                    Intent intent = getIntent();
                    intent.putExtra("note", note);
                    setResult(ACTIVITY_UPDATE, intent);
                    finish();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(AddNoteActivity.this, BmobErrorCodeUtils.getMessage(i, s), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {


    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        tempCount = s;
    }

    @Override
    public void afterTextChanged(Editable s) {
        int number = totalNum - s.length();
        textNum.setText(totalNum - number + "/" + totalNum);
        selectionStart = content.getSelectionStart();
        selectionEnd = content.getSelectionEnd();
        if (tempCount.length() > totalNum) {
            s.delete(selectionStart - 1, selectionEnd);
            int tempSelection = selectionEnd;
            content.setText(s);
            content.setSelection(tempSelection);//设置光标在最后
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }
}
